package cn.suhaoblog.oss.config;

import cn.suhaoblog.oss.entity.OSSConfigBean;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author rambler
 * @since 2020-12-23 23:13
 */
@Configuration
@Slf4j
@EnableConfigurationProperties(OSSConfigBean.class)
public class OSSConfig {

    @Resource
    private OSSConfigBean config;

    @Bean
    public OSS ossClient() {
        try {
            // 启动时读取yaml中oss配置, 注入到spring Bean中, 读取Bean配置创建OSSClient实例。
            OSS ossClient = new OSSClient(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());
            log.info("初始化OSS Client成功");
            return ossClient;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
}
